// J2ME Compass
// Copyright (C) 2006 Dana Peters
// http://www.qcontinuum.org/compass

package org.qcontinuum.astro;

import java.lang.Math;
import javax.microedition.lcdui.*;
import henson.midp.Float;

public class HorizontalPosition {

    private Float mAzimuth;
    private Float mElevation;

    public HorizontalPosition() {
        mAzimuth = new Float(0);
        mElevation = new Float(0);
    }

    public HorizontalPosition(int azimuth, int elevation) {
        mAzimuth = new Float(azimuth);
        mElevation = new Float(elevation);
    }

    public HorizontalPosition(Float azimuth, Float elevation) {
        mAzimuth = azimuth;
        mElevation = elevation;
    }

    public int getAzimuth() {
        return (int)Float.Int(mAzimuth).toLong();
    }
    
    public int getElevation() {
        return (int)Float.Int(mElevation).toLong();
    }

    public Float getAzimuthFloat() {
        return mAzimuth;
    }
    
    public Float getElevationFloat() {
        return mElevation;
    }

    public void setAzimuth(int azimuth) {
        mAzimuth = new Float(azimuth);
    }
    
    public void setElevation(int elevation) {
        mElevation = new Float(elevation);
    }

    public ScreenPosition toScreenPosition(Canvas canvas, int offset, boolean flipX) {
        int midX = canvas.getWidth() / 2;
        int midY = canvas.getHeight() / 2;
        int maxR = Math.min(midX, midY);
        ScreenPosition screenPosition = new ScreenPosition();
        Float r = new Float(90).Sub(mElevation).Div(90).Mul(maxR);
        Float azimuth = Float.toRadians(mAzimuth.Sub(new Float(offset)));
        screenPosition.x = (int)(Float.sin(azimuth).Mul(r).toLong()) * (flipX ? -1 : 1) + midX;
        screenPosition.y = (int)Float.cos(azimuth).Mul(r).Neg().toLong() + midY;
        return screenPosition;
    }
}
